class RecommObject
  attr :keywords_hash
  
  # konstruktor
  def initialize
    @keywords_hash = Hash.new
  end
  
  # usporedba s drugim RecommObjektom
  def compare( recomm_object )
    
    result = RecommObject.new
    
    self.keywords_hash.each do |key, value|
      
      if recomm_object.keywords_hash.has_key?(key)
        result.keywords_hash.store(key, value * recomm_object.keywords_hash[key] )
      end
      
    end
        
    similarty_factor = result.sum / ( self.modulo * recomm_object.modulo)
    
    return {'factor' => similarty_factor, 'result' => result} 
    
  end
  
  # Suma svih keywordova
  def sum
    sum = 0
    
    @keywords_hash.each_value do |value|
      sum += value
    end 
    
    return sum
  end
  
  # Modulo odnosno korijen iz sume kvadrata keywordova
  def modulo
    
    sum = 0
    
    @keywords_hash.each_value do |value|
      sum += value * value
    end
    
    return Math.sqrt sum
    
  end
  
  # Ispis N keywordova sa brojem na ekran
  def print_keywords_hash( n = 10)
    
    @keywords_hash.sort {|a,b| b[1]<=>a[1]}.each do |key,value|
      puts "#{key}: #{value}"
      n -= 1
      break if n <= 0
    end
  end
  
  # Povecaj keyword za X ili ga stvori ako ne postoji
  def increment_keyword ( keyword, change)
    
    if @keywords_hash.has_key? keyword
      @keywords_hash[keyword] += change
    else
      @keywords_hash[keyword] = change
    end
    
  end
end